
package com.ami.kvm.jviewer.kvmpkts;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.gui.JVFrame;
import com.ami.kvm.jviewer.gui.JVMenu;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.JViewerView;
import com.ami.kvm.jviewer.gui.JViewerView.RCMouseListener;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import com.ami.kvm.jviewer.hid.USBMouseRep;
import com.ami.kvm.jviewer.jvvideo.JVVideo;
import java.awt.AWTException;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.Timer;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

public class Mousecaliberation {
	public static boolean Threshold_Started = false;
	public static int PREV_ACCEL_THRESHOLD = 4;
	public static float PREV_MOUSE_ACCELERATION = 2.0F;
	public static boolean THRESHOLDFLAG = false;
	public static boolean ACCELERATION_FLAG = false;

	RCMouseListener m_mouseListener;

	RCKeyListener m_keyListener;
	public static Robot robbie;
	static int curX = 0;
	static int curY = 0;
	static int lastX = -1;
	static int lastY = -1;

	public static int i;
	public static int j;
	public static final String THRESH_MSG = LocaleStrings.getString("3_1_MC");
	public static final String ACCEL_MSG = LocaleStrings.getString("3_2_MC");
	public static final String THRESH_CONF = LocaleStrings.getString("3_3_MC");
	public static final String ACCEL_CONF = LocaleStrings.getString("3_4_MC");

	private Timer m_frTmr1;
	private Timer m_frTmr2;
	private boolean first_time = true;
	private static boolean cursorReset = true;

	public Mousecaliberation() {
		try {
			this.m_mouseListener = RCMouseListener.class.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		this.m_keyListener = new RCKeyListener();
	}

	public void OnCalibareteMouseThreshold(boolean paramBoolean) {
		JVFrame localJVFrame1 = JViewerApp.getInstance().getMainWindow();
		if (Threshold_Started) {
			OnCalibareteMouseAcceleration(false);
			Threshold_Started = false;
			return;
		}

		if (this.first_time) {
			JViewerApp.getInstance().getRCView().addKeyListener(this.m_keyListener);
			JViewerApp.getInstance().getRCView().addMouseListener(this.m_mouseListener);
			JViewerApp.getInstance().getRCView().addMouseMotionListener(this.m_mouseListener);
			this.first_time = false;
		}

		if (paramBoolean) {
			if (JViewerApp.getInstance().getRCView().GetUSBMouseMode() == USBMouseRep.RELATIVE_MOUSE_MODE) {
				if (robbie == null) {
					try {
						robbie = new Robot();
					} catch (AWTException localAWTException) {
						System.err.println(LocaleStrings.getString("3_11_MC"));
						Debug.out.println(localAWTException);
						robbie = null;
						return;
					}
				}
			}

			JViewerApp.getInstance().getJVMenu().notifyMenuStateEnable("VideoPauseRedirection", false);

			THRESHOLDFLAG = true;
			PREV_ACCEL_THRESHOLD = JViewerView.ACCEL_THRESHOLD;
			PREV_MOUSE_ACCELERATION = JViewerView.MOUSE_ACCELERATION;
			JViewerView.ACCEL_THRESHOLD = 1;
			JViewerView.MOUSE_ACCELERATION = 1.0F;
			JViewerApp.getInstance().OnShowCursor(false);
			ShowCursor_caliberate(true);
			resetCursor();

			JOptionPane.showMessageDialog(localJVFrame1, THRESH_MSG, LocaleStrings.getString("3_5_MC"), 1);

			this.m_frTmr2 = new Timer();
			this.m_frTmr2.schedule(new FrameRateTask1(), 0L, 750L);
		} else {
			JVFrame localJVFrame2;
			int k;
			if (Threshold_Started) {
				this.m_frTmr1.cancel();
				robbie = null;
				JViewerApp.getInstance().getJVMenu().menu_string.setText("");
				localJVFrame2 = JViewerApp.getInstance().getMainWindow();
				UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
				k = JOptionPane.showConfirmDialog(localJVFrame2, ACCEL_CONF, LocaleStrings.getString("3_5_MC"), 0);

				if (k == 0) {
					resetCursor();
					JViewerApp.getInstance().getRCView().removeKeyListener(this.m_keyListener);
					JViewerApp.getInstance().getRCView().removeMouseListener(this.m_mouseListener);
					JViewerApp.getInstance().getRCView().removeMouseMotionListener(this.m_mouseListener);
					OnCalibareteMouseAcceleration(true);
				}

				if ((k == 1) || (k == -1)) {
					JViewerView.MOUSE_ACCELERATION = PREV_MOUSE_ACCELERATION;
					JViewerView.ACCEL_THRESHOLD = PREV_ACCEL_THRESHOLD;
					JViewerApp.getInstance().getRCView().removeKeyListener(this.m_keyListener);
					JViewerApp.getInstance().getRCView().removeMouseListener(this.m_mouseListener);
					JViewerApp.getInstance().getRCView().removeMouseMotionListener(this.m_mouseListener);
					JViewerApp.getInstance().getJVMenu().notifyMenuStateEnable("VideoPauseRedirection", true);
					ShowCursor_caliberate(false);
				}

			} else {
				this.m_frTmr2.cancel();
				robbie = null;
				JViewerApp.getInstance().OnShowCursor(false);
				JViewerApp.getInstance().getJVMenu().menu_string.setText("");
				localJVFrame2 = JViewerApp.getInstance().getMainWindow();
				UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
				k = JOptionPane.showConfirmDialog(localJVFrame2, THRESH_CONF, LocaleStrings.getString("3_5_MC"), 0);

				if (k == 0) {
					JViewerApp.getInstance().getRCView().mouse_resync_linux_mode(0, 0);
					JViewerApp.getInstance().getJVMenu().notifyMenuStateEnable("VideoPauseRedirection", true);
					Threshold_Started = true;
					JViewerView.MOUSE_ACCELERATION = PREV_MOUSE_ACCELERATION;
					OnCalibareteMouseAcceleration(true);
				}

				if ((k == 1) || (k == -1)) {
					JViewerView.ACCEL_THRESHOLD = PREV_ACCEL_THRESHOLD;
					JViewerView.MOUSE_ACCELERATION = PREV_MOUSE_ACCELERATION;
					JViewerApp.getInstance().getJVMenu().notifyMenuStateEnable("VideoPauseRedirection", true);
					JViewerApp.getInstance().getRCView().removeKeyListener(this.m_keyListener);
					JViewerApp.getInstance().getRCView().removeMouseListener(this.m_mouseListener);
					JViewerApp.getInstance().getRCView().removeMouseMotionListener(this.m_mouseListener);
					JViewerApp.getInstance().getRCView().addKMListeners();
					JViewerApp.getInstance().getVidClnt().setFullScreenMode();
					this.first_time = true;
					ShowCursor_caliberate(false);
				}
				THRESHOLDFLAG = false;
			}
		}
	}

	public void ShowCursor_caliberate(boolean paramBoolean) {
		URL localURL = JViewer.class.getResource("res/hvcur.gif");
		BufferedImage localBufferedImage = null;
		try {
			if (paramBoolean) {
				localBufferedImage = ImageIO.read(localURL);
			} else
				localBufferedImage = new BufferedImage(32, 32, 3);
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
		}
		Cursor localCursor = Toolkit.getDefaultToolkit().createCustomCursor(localBufferedImage, new Point(0, 0),
				"Invisible Cursor");

		JViewerApp.getInstance().getRCView().setCursor(localCursor);
	}

	public void OnCalibareteMouseAcceleration(boolean paramBoolean) {
		JVFrame localJVFrame1 = JViewerApp.getInstance().getMainWindow();
		if (paramBoolean) {
			if (robbie == null) {
				try {
					robbie = new Robot();
				} catch (AWTException localAWTException) {
					System.err.println(LocaleStrings.getString("3_11_MC"));
					Debug.out.println(localAWTException);
					robbie = null;
					return;
				}
			}

			PREV_MOUSE_ACCELERATION = JViewerView.MOUSE_ACCELERATION;
			ShowCursor_caliberate(true);
			if (JViewerView.Lost_focus_flag)
				JViewerView.Lost_focus_flag = false;
			resetCursor();
			ACCELERATION_FLAG = true;
			JViewerApp.getInstance().getJVMenu().notifyMenuStateEnable("VideoPauseRedirection", false);
			JViewerView.MOUSE_ACCELERATION = 1.0F;
			JOptionPane.showMessageDialog(localJVFrame1, ACCEL_MSG, LocaleStrings.getString("3_5_MC"), 1);

			JViewerApp.getInstance().getJVMenu().getMenuItem("Calibrate Mouse Threshold").setSelected(true);
			this.m_frTmr1 = new Timer();
			this.m_frTmr1.schedule(new FrameRateTask(), 0L, 750L);

		} else {

			this.m_frTmr1.cancel();
			ACCELERATION_FLAG = false;
			robbie = null;
			JViewerApp.getInstance().OnShowCursor(false);
			JViewerApp.getInstance().getJVMenu().menu_string.setText("");

			JVFrame localJVFrame2 = JViewerApp.getInstance().getMainWindow();
			UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
			int k = JOptionPane.showConfirmDialog(localJVFrame2, ACCEL_CONF, LocaleStrings.getString("3_5_MC"), 0);

			if (k == 0) {
				JViewerApp.getInstance().getJVMenu().notifyMenuStateEnable("VideoPauseRedirection", true);
				JViewerApp.getInstance().getRCView().removeKeyListener(this.m_keyListener);
				JViewerApp.getInstance().getRCView().removeMouseListener(this.m_mouseListener);
				JViewerApp.getInstance().getRCView().removeMouseMotionListener(this.m_mouseListener);
			}
			if (k == 1) {
				JViewerView.MOUSE_ACCELERATION = PREV_MOUSE_ACCELERATION;
				JViewerApp.getInstance().getJVMenu().notifyMenuStateEnable("VideoPauseRedirection", true);
				JViewerApp.getInstance().getRCView().removeKeyListener(this.m_keyListener);
				JViewerApp.getInstance().getRCView().removeMouseListener(this.m_mouseListener);
				JViewerApp.getInstance().getRCView().removeMouseMotionListener(this.m_mouseListener);
			}
			this.first_time = true;
			JViewerApp.getInstance().getJVMenu().notifyMenuStateSelected("Calibrate Mouse Threshold", false);
			resetCursor();
			ShowCursor_caliberate(false);
		}

		JViewerApp.getInstance().getVidClnt().setFullScreenMode();
	}

	public static boolean isCursorReset() {
		return cursorReset;
	}

	public static void setCursorReset(boolean paramBoolean) {
		cursorReset = paramBoolean;
	}

	public static void resetCursor() {
		if ((!JViewerApp.getInstance().getVidClnt().isNewFrame()) || (JViewerView.Lost_focus_flag)) {
			cursorReset = false;
			return;
		}
		int k = JViewerApp.getInstance().getRCView().viewWidth();
		int m = JViewerApp.getInstance().getRCView().viewHeight();
		int n = JViewerApp.getInstance().getMainWindow().getM_viewSP().getHorizontalScrollBar().getValue();
		int i1 = JViewerApp.getInstance().getMainWindow().getM_viewSP().getVerticalScrollBar().getValue();
		if ((THRESHOLDFLAG) || (ACCELERATION_FLAG)) {
			JViewerApp.getInstance().getRCView().mouseListener.splitandsend(-k, -m, true);

			JViewerApp.getInstance().getRCView().mouseListener.splitandsend(n / 2, i1 / 2, true);
			cursorReset = true;
			i = getViewPoint().x;
			j = getViewPoint().y;
			if (robbie == null) {
				try {
					robbie = new Robot();
				} catch (AWTException localAWTException) {
					System.err.println(LocaleStrings.getString("3_11_MC"));
					Debug.out.println(localAWTException);
					robbie = null;
					return;
				}
			}
			robbie.mouseMove(i + 1, j + 1);
		} else {
			JViewerApp.getInstance().getRCView().mouseListener.splitandsend_Move(-k, -m, true);

			JViewerApp.getInstance().getRCView().mouseListener.splitandsend_Move((n + 1) / 2, (i1 + 1) / 2, true);
			cursorReset = true;
		}
	}

	public static Point getViewPoint() {
		Point localPoint;

		if (JViewerApp.getInstance().isFullScreenMode()) {
			localPoint = JViewerApp.getInstance().getRCView().getLocationOnScreen();
		} else
			localPoint = JViewerApp.getInstance().getMainWindow().getM_viewSP().getLocationOnScreen();
		return localPoint;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\kvmpkts\
 * Mousecaliberation.class Java compiler version: 5 (49.0) JD-Core Version:
 * 0.7.1
 */